<?php
namespace Service;

class UserAccount extends Base
{

    private $database = null;

    public function getDatabase()
    {
        if($this->database){
            return $this->database;
        }
        $this->database = $this->get(DATABASE_USERACCOUNT);
        return $this->database;
    }

    public function addAccount($userId, $addOrSub, $type, $amount, $note = '', $more = [], $transactions = [])
    {
        $userId = (int)$userId;
        if($userId < 0){
            return false;
        }
        if(!in_array($addOrSub, [ACCOUNT_ADD, ACCOUNT_SUB])){
            return false;
        }
        if(!checkAccountLogType($type)){
            return false;
        }
        if(!is_numeric($amount) || $amount == 0){
            return false;
        }
        $now = getDateTime();
        $insertOrUpdate = [];
        $amountAbs = abs($amount);
        $amountNegtive = 0 - $amountAbs;
        $userAccount = $this->one($userId);
        if($userAccount){
            if($addOrSub == ACCOUNT_SUB && $userAccount->dry_balance < $amountAbs){
                return false;
            }
            if($addOrSub == ACCOUNT_ADD){
                $insertOrUpdate = [
                    'service' => SERVICE_USERACCOUNT,
                    'action' => SQL_UPDATE,
                    'update' => [
                        '&dry_balance' => "dry_balance + {$amountAbs}",
                        '&dry_total' => "dry_total + {$amountAbs}",
                        'dry_update_time' => $now['date_time']
                    ],
                    'updateId' => $userAccount->id
                ];
            }
            else if($addOrSub == ACCOUNT_SUB){
                $insertOrUpdate = [
                    'service' => SERVICE_USERACCOUNT,
                    'action' => SQL_UPDATE,
                    'update' => [
                        '&dry_balance' => "dry_balance - {$amountAbs}",
                        'dry_update_time' => $now['date_time']
                    ],
                    'updateId' => $userAccount->id
                ];
            }
        }
        else{
            if($addOrSub == ACCOUNT_SUB){
                return false;
            }
            $insertOrUpdate = [
                'service' => SERVICE_USERACCOUNT,
                'action' => SQL_INSERT,
                'insert' => [
                    'id' => $userId,
                    'dry_balance' => $amountAbs,
                    'dry_total' => $amountAbs,
                    'dry_add_date' => $now['date'],
                    'dry_add_time' => $now['date_time'],
                    'dry_update_time' => $now['date_time']
                ]
            ];
        }
        $ext = [
            $insertOrUpdate,
            [
                'service' => SERVICE_USERACCOUNTLOG,
                'action' => SQL_INSERT,
                'insert' => [
                    'dry_user' => $userId,
                    'dry_invited' => 0,
                    'dry_type' => $type,
                    'dry_amount' => ($addOrSub == ACCOUNT_ADD)?$amountAbs:$amountNegtive,
                    'dry_order_no' => '',
                    'dry_month' => date('Ym', getTimestamp()),
                    'dry_add_date' => $now['date'],
                    'dry_add_time' => $now['date_time'],
                    'dry_note' => $note
                ]
            ]
        ];
        foreach($transactions as $transaction){
            $ext[] = $transaction;
        }
        return $this->transaction(SERVICE_USERACCOUNT, $ext);
    }

}